package com.bit.cfdi.web.dao.impl;

import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.bit.cfdi.web.dao.ContactoDAO;
import com.bit.cfdi.web.domain.Contacto;

@Repository
public class ContactoDAOImpl extends HibernateDaoSupport implements ContactoDAO {

	@Autowired
	public void init(final SessionFactory sessionFactory) {
		setSessionFactory(sessionFactory);
	}
	
	@Transactional
	@Override
	public void save(final Contacto contacto) throws DataAccessException {
		getHibernateTemplate().saveOrUpdate(contacto);
	}
	
	@Transactional
	@Override
	public void delete(final Contacto contacto) throws DataAccessException {
		getHibernateTemplate().delete(contacto);
	}

	@Transactional
	@SuppressWarnings("unchecked")
	@Override
	public Contacto getById(final Long id) throws DataAccessException {
		List<Contacto> contactos = getHibernateTemplate().find(
				" from Contacto as c" +
				" where c.idContacto=?", id);
		return contactos.size()>0?contactos.get(0):null;
	}

	@Transactional
	@SuppressWarnings("unchecked")
	@Override
	public List<Contacto> getAll() throws DataAccessException {
		List<Contacto> contactos = getHibernateTemplate().find(
				" from Contacto as c");
		return contactos;
	}

}
